package inet.app.controller.database;

import inet.app.model.object.Tk_Item;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class DaoThongKeNhanh extends DaoBase {

	private final static String selection = DBConfig.COL_TK_NHANH_CODE + "=?"
			+ " and " + DBConfig.COL_TK_NHANH_NUMBERDATE + "=?" + " and "
			+ DBConfig.COL_TK_NHANH_NUMBERCOUPLE;

	public DaoThongKeNhanh(Context context) {
		super(context);
	}

	private ContentValues createContentValueFromRow(Tk_Item row) {

		ContentValues value = new ContentValues();
		value.put(DBConfig.COL_TK_NHANH_CODE, row.getCode());
		value.put(DBConfig.COL_TK_NHANH_NAME, row.getName());
		value.put(DBConfig.COL_TK_NHANH_NUMBERDATE, row.getNumberdate());
		value.put(DBConfig.COL_TK_NHANH_NUMBERCOUPLE, row.getNumbercouple());
		value.put(DBConfig.COL_TK_NHANH_TKCS, row.getTkcs());
		value.put(DBConfig.COL_TK_NHANH_MOST, row.getMost());
		value.put(DBConfig.COL_TK_NHANH_LEAST, row.getLeast());
		value.put(DBConfig.COL_TK_NHANH_NUMBER, row.getNumber());
		value.put(DBConfig.COL_TK_NHANH_DATE, row.getDate());
		value.put(DBConfig.COL_TK_NHANH_CHUARA, row.getChuara());
		value.put(DBConfig.COL_TK_NHANH_CUCDAI, row.getCucdai());
		return value;
	}

	private Tk_Item createRowFromCursor(Cursor cursor) {

		Tk_Item row = new Tk_Item();
		row.setId(cursor.getInt(cursor.getColumnIndex(DBConfig.COL_ID)));
		row.setCode(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_CODE)));
		row.setName(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_NAME)));
		row.setNumberdate(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_NUMBERDATE)));
		row.setNumbercouple(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_NUMBERCOUPLE)));
		row.setTkcs(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_TKCS)));
		row.setMost(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_MOST)));
		row.setLeast(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_LEAST)));
		row.setNumber(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_NUMBER)));
		row.setDate(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_DATE)));
		row.setChuara(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_CHUARA)));
		row.setCucdai(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_TK_NHANH_CUCDAI)));
		return row;
	}

	public long insertRow(Tk_Item row) {
		if (!dbConnection.openSession())
			return -1;
		ContentValues values = createContentValueFromRow(row);
		long insertId = dbConnection.insert(DBConfig.TABLE_TK_NHANH, null,
				values);
		dbConnection.closeSession();
		return insertId;
	}

	public int deleteRow(int id) {
		int res = -1;
		if (!dbConnection.openSession())
			return res;
		Cursor cursor = dbConnection.query(DBConfig.TABLE_TK_NHANH, null,
				DBConfig.COL_ID + "=?", new String[] { String.valueOf(id) },
				null, null, null);
		if (cursor != null) {
			int rows = cursor.getCount();
			if (rows > 0) {
				/** row is existed, then delete row */
				res = dbConnection.delete(DBConfig.TABLE_TK_NHANH,
						DBConfig.COL_ID + "=?",
						new String[] { String.valueOf(id) });
			} else {
				/** row is not existed */
				res = 0;
			}
		}
		cursor.close();
		dbConnection.closeSession();
		return res;
	}

	public int updateRow(Tk_Item row) {
		int res = -1;
		if (row == null) {
			return res;
		}
		String numberDate = row.getNumberdate();
		String code = row.getCode();
		String couple = row.getNumbercouple();
		Tk_Item old = selectRow(code, numberDate, couple);
		if (old == null)
			return res;
		Cursor cursor = dbConnection.query(DBConfig.TABLE_TK_NHANH, null,
				selection, new String[] { code, numberDate, couple }, null, null,
				null);
		if (cursor != null) {
			int rows = cursor.getCount();
			if (rows > 0) {
				/** row is existed, then update row */
				ContentValues values = createContentValueFromRow(row);
				res = dbConnection.update(DBConfig.TABLE_TK_NHANH, values,
						DBConfig.COL_ID + "=?",
						new String[] { row.getId() + "" });
			} else {
				/** row is not existed */
				res = -1;
			}
		}
		cursor.close();
		dbConnection.closeSession();
		return res;
	}

	/**
	 * find row by id
	 * 
	 * @param fileId
	 * @return
	 */
	public Tk_Item selectRow(String code, String numberDate, String couple) {
		Tk_Item row = null;
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return row;
			cursor = dbConnection.query(DBConfig.TABLE_TK_NHANH, null,
					selection, new String[] { code, numberDate, couple }, null, null,
					null);
			if (cursor != null) {
				int rows = cursor.getCount();
				if (rows > 0) {
					/** row is existed, then get row info */
					cursor.moveToFirst();
					row = createRowFromCursor(cursor);
				}
			}
			cursor.close();
			dbConnection.closeSession();
		} catch (Exception ex) {
			dbConnection.closeSession();
		}
		return row;
	}

	public long insertCollection(List<Tk_Item> rows) {
		if (rows == null) {
			return -1;
		}
		List<ContentValues> values = new ArrayList<ContentValues>();
		for (Tk_Item row : rows) {
			ContentValues value = createContentValueFromRow(row);
			values.add(value);
		}
		return insertCollection(DBConfig.TABLE_TK_NHANH, values);
	}

	public int deleteAll() {
		return deleteAll(DBConfig.TABLE_TK_NHANH);
	}

}
